<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>菜单管理</title>
    <link rel="stylesheet" href="../../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../css/public.css" media="all">
    <style>
        .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
            height: 34px;
            line-height: 34px;
            padding: 0 8px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <a class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="rbac:menu:add"> 添加 </a>
                <a class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" id="btn-expand"> 全部展开 </a>
                <a class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" id="btn-fold"> 全部折叠 </a>
            </div>
        </script>

        <table id="menu-table" class="layui-table" lay-filter="menu-table"></table>

        <!-- 操作列 -->
        <script type="text/html" id="auth-state">
            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="rbac:menu:addNew">添加</a>
            <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="rbac:menu:edit">修改</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="rbac:menu:del">删除</a>
        </script>
    </div>
</div>

<script src="../../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="../../../js/lay-config.js" charset="utf-8"></script>
<script>
    layui.use(['table', 'treetable'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var treetable = layui.treetable;

        window.renderTable = function() {
            // 渲染表格
            layer.load(2);
            treetable.render({
                treeColIndex: 1,
                treeSpid: -9,
                treeIdName: 'pkId',
                treePidName: 'parentId',
                elem: '#menu-table',
                url: 'admin/rbac/menu/main',
                toolbar: '#toolbarDemo',
                page: false,
                cols: [[
                    {type: 'numbers'},
                    {field: 'menuName', minWidth: 200, title: '权限名称'},
                    {field: 'authorityKey', title: '权限标识'},
                    {field: 'menuUrl', title: '菜单URL'},
                    {
                        field: 'isMenu', width: 220, align: 'center', title: '类型', templet: function (d) {
                            if (d.menuType == 1) {
                                return '<span class="layui-badge layui-bg-gray">目录</span>';
                            } else if (d.menuType == 2) {
                                return '<span class="layui-badge layui-bg-blue">菜单</span>';
                            } else {
                                return '<span class="layui-badge-rim">按钮</span>';
                            }
                        }
                    },
                    {
                        field: 'isShow', width: 120, align: 'center', title: '是否显示', templet: function (d) {
                            if (d.isShow == 1) {
                                return '<span class="layui-badge layui-bg-blue">显示</span>';
                            } else {
                                return '<span class="layui-badge-rim">隐藏</span>';
                            }
                        }
                    },
                    {
                        field: 'menuStatus', width: 120, align: 'center', title: '状态', templet: function (d) {
                            if (d.menuStatus == 1) {
                                return '<span class="layui-badge layui-bg-blue">启用</span>';
                            } else {
                                return '<span class="layui-badge-rim">禁用</span>';
                            }
                        }
                    },
                    {field: 'orderNo', width: 180, align: 'center', title: '排序号'},
                    {templet: '#auth-state', width: 240, align: 'center', title: '操作'}
                ]],
                done: function () {
                    layer.closeAll('loading');
                }
            });
        };

        renderTable();

        $('#btn-expand').click(function () {
            treetable.expandAll('#menu-table');
        });

        $('#btn-fold').click(function () {
            treetable.foldAll('#menu-table');
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(menu-table)', function (obj) {
            if (obj.event === 'rbac:menu:add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加权限',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    area: ['40%', '80%'],
                    content: 'add.html'
                });
            } else if (obj.event === 'rbac:menu:batcgdel') {  // 监听删除操作
                var checkStatus = main.checkStatus('currentTableId')
                    , data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
        });

        //监听工具条
        table.on('tool(menu-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'rbac:menu:del') {
                layer.confirm('确定要删除?', {icon: 3, title:'提示'}, function(index){
                    $.ajax({
                        type: 'DELETE',
                        url: 'admin/rbac/menu/'+data.pkId,
                        contentType: 'application/json',
                        success:function(result){
                            if(result.code==0){
                                layer.alert("删除成功");
                                renderTable();
                            }else{
                                layer.alert(result.msg);
                            }
                        }
                    });
                });
            } else if (layEvent === 'rbac:menu:edit') {
                layer.open({
                    title: '添加权限',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    area: ['40%', '80%'],
                    content: 'edit.html',
                    success: function(layero, index){
                        var iframe = window['layui-layer-iframe' + index];
                        iframe.setValue(data.pkId);
                    }
                });
            } else if (layEvent === 'rbac:menu:addNew') {
                if(data.menuType===3){
                    layer.alert("按钮下不允许添加子节点");
                }else{
                    layer.open({
                        title: '添加权限',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        area: ['40%', '80%'],
                        content: 'add.html',
                        success: function(layero, index){
                            var iframe = window['layui-layer-iframe' + index];
                            iframe.setValue(data);
                        }
                    });
                }
            }
        });
    });
</script>
</body>
</html>